package com.example.idachuappone.order.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.idachuappone.R;
import com.example.idachuappone.order.imageutil.BitmapCache;
import com.example.idachuappone.order.imageutil.BitmapCache.ImageCallback;

public class AdapterUploading extends BaseAdapter {
	BitmapCache cache;
	ImageCallback callback;
	private Context context;
	private List<String> list;

	private class ViewHolder {
		ImageView img;

		private ViewHolder() {
		}
	}

	public AdapterUploading(Context context) {
		this.callback = new ImageCallback() {
			@Override
			public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
				if (imageView != null && bitmap != null) {
					String url = (String) params[0];
					if (url != null && url.equals(imageView.getTag())) {
						imageView.setImageBitmap(bitmap);
					}
				}
			}
		};
		this.context = context;
		this.cache = new BitmapCache();
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (String) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LinearLayout.inflate(this.context, R.layout.simple_uploading_img, null);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (this.list.get(position) == null || this.list.get(position).length() <= 0) {
			holder.img.setTag("");
			holder.img.setImageResource(R.drawable.icon_carmera);
		} else {
			String sourcePath = this.list.get(position);
			holder.img.setTag(sourcePath);
			this.cache.displayBmp(holder.img, null, sourcePath, this.callback);
		}
		return convertView;
	}
}
